﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Data;
//using System.Windows.Documents;
//using System.Windows.Input;
//using System.Windows.Media;
//using System.Windows.Media.Imaging;
//using System.Windows.Shapes;

//namespace Sokoban
//{
//    /// <summary>
//    /// Interaction logic for GameWindow.xaml
//    /// </summary>
//    public partial class GameWindow : Window
//    {
//        public GameWindow()
//        {
//            InitializeComponent();
//        }
//    }
//}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sokoban.Model;
using Sokoban.ViewModel;
using Microsoft.Win32;
using System.IO;

namespace Sokoban
{
    /// <summary>
    /// Interaction logic for GameWindow.xaml
    /// </summary>
    public partial class GameWindow : Window
    {
        public GameWindow(string path)
        {           
            InitializeComponent();

            myViewModel = new SokobanViewModel();
            this.DataContext = myViewModel;         

            try
            {
                myViewModel.LoadMap(path);
            }
            catch (Exception exception)
            {
                MessageBox.Show("Invalid map!");
                return;
            }               

            // Grid létrehozása viewModel.BoxMatrix alapjén

            // Viewmodelek létrehozása
            // bindingok
            // Viewmodelekbe kerüljön a logika

            InitializeGrid();
        }

        private void InitializeGrid()
        {
            // set rows and columns
            for (int i = 0; i < myViewModel.Rows; i++)
            {
                var rowDef = new RowDefinition { Height = new GridLength(1, GridUnitType.Star) };
                GameMap.RowDefinitions.Add(rowDef);
            }

            for (int j = 0; j < myViewModel.Cols; j++)
            {
                var colDef = new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) };
                GameMap.ColumnDefinitions.Add(colDef);
            }

            // fill rows and columns
            for (int i = 0; i < myViewModel.Rows; i++)
            {
                for (int j = 0; j < myViewModel.Cols; j++)
                {                    
                    var image = new Image();

                    var imgBindig = new Binding { Path = new PropertyPath("ImgSource"), Source = myViewModel.BoxMatrix[i, j] };
                    BindingOperations.SetBinding(image, Image.SourceProperty, imgBindig);

                    GameMap.Children.Add(image);
                    Grid.SetRow(image, i);
                    Grid.SetColumn(image, j);

                }
            }
        }

        private SokobanViewModel myViewModel;

        private void GameWindow_OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Up)
            {
                myViewModel.MoveUp();
                return;
            }
            if (e.Key == Key.Down)
            {
                myViewModel.MoveDown();
                return;
            }
            if (e.Key == Key.Left)
            {
                myViewModel.MoveLeft();
                return;
            }
            if (e.Key == Key.Right)
            {
                myViewModel.MoveRight();
                return;
            }
        }
    }
}

